home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Demos / A.D. Software / OOFILE 1.3b4d6.sit / OOFILE 1.3b4d6 / MacCodeWarriorDemo1.3b4d6 / docs / samples / ooftst31.cpp < prev    next >
C/C++ Source or Header  |  1997-03-17  |  3KB  |  103 lines

  1. // Copyright 1996 A.D. Software. All Rights Reserved
  2.  
  3. // OOFTEST31
  4.  
  5. // Simple stream I/O is used to interact with the user.
  6. // to demonstrate the storage of blobs in a c-tree database
  7.  
  8. #include "oofile.h"
  9.  
  10. DECLARE_CLASS(dbImageViewer)
  11.         dbChar          FileName;
  12.         dbDate            FileModDate;
  13.         dbChar          Description;
  14.         dbBLOB          Thumbnail;
  15.  
  16.         dbImageViewer() : dbTable("ImagView") ,
  17.                                 FileName(50, "File Name"),
  18.                                 FileModDate("Mod Date"),
  19.                                 Description(100, "Description"),
  20.                                 Thumbnail("Thumbnail")
  21.         {
  22.                 FileName.index(kIndexCompress);
  23.                 FileModDate.index(kIndexCompress);
  24.                 Description.index(kIndexCompress);
  25.                 setSortOrder(FileName);
  26.         };
  27.         
  28.         void addTestData();
  29. };
  30.  
  31. void
  32. dbImageViewer::addTestData()
  33. {
  34.     long testBuf[] = {0, 2, 4, 8, 16, 65535, 0xADADADAD};
  35.     long bufLen = sizeof(testBuf);
  36.     long* copiedBuf = new long[bufLen/sizeof(long)];
  37.     memcpy(copiedBuf, testBuf, bufLen);
  38.     
  39. // just add two records
  40.     newRecord();
  41.     FileName = "first blob";
  42.     FileModDate = dbDate::currentDate();
  43.     Description = "0, 2, 4, 8, 16, 65535, 0xADADADAD";
  44.     Thumbnail.adoptBody(copiedBuf, bufLen);    // transfer ownership of this memory, not copy!
  45.     saveRecord();
  46.     
  47.     newRecord();
  48.     FileName = "second blob";
  49.     FileModDate = dbDate::currentDate()+1;
  50.     Description = "0, 2, 4, 8, 16, 65535, 0xADADADAD repeated";
  51.     Thumbnail.setBytes(testBuf, bufLen);
  52. // would cause assertion failure    Thumbnail += Thumbnail;
  53.     Thumbnail.append(testBuf, bufLen);
  54.     saveRecord();
  55.     
  56. }
  57.  
  58.  
  59. int 
  60. main()
  61. {
  62.     cout << "OOFILE Validation Suite - Test 31\n"
  63.          << "Store blobs in database"
  64.          << endl << endl;
  65.  
  66.     TEST_CONNECT    theDB;
  67.     dbImageViewer     Images;    // not really - just arbitrary blobs for now!
  68.  
  69.     #ifdef TESTING_DBASE
  70.         #ifdef _Macintosh
  71.             const char* kExistsName =  ":ooftst31:ImagView.dbf";
  72.             const char* kDatabaseName = ":ooftst31:";
  73.         #else
  74.             const char* kExistsName =   "ImagView.dbf"
  75.             const char* kDatabaseName = "";
  76.         #endif    
  77.  
  78.     #else
  79.         const char* kDatabaseName = "ooftst31.db";
  80.         const char* kExistsName = kDatabaseName;
  81.     #endif
  82.     
  83.  
  84.     if (dbConnect::fileExists(kExistsName)) {
  85.         theDB.openConnection(kDatabaseName);
  86.         Images.deleteAll();
  87.     }
  88.     else {
  89.         theDB.newConnection(kDatabaseName);
  90.     }
  91.     Images.addTestData();
  92.     
  93.     cout << "Dump of entire database\n" << Images << endl;
  94.     
  95.     Images.start();
  96.     Images.Thumbnail.setBytes("ABC", 3);
  97.     Images.saveRecord();
  98.     cout << "Modify BLOB in first record to 'ABC' (hex 41, 42, 43)\n" << Images << endl;
  99.     
  100.     cout << endl << "done" << endl;
  101.     return EXIT_SUCCESS;
  102. }       
  103.